﻿using System;

namespace Dotnet.Utils.Utility.ObjectExtensions.To
{
    public static class Parser
    {
        public static bool ToBoolean(string s, bool defaultVal)
        {
            bool flag;
            bool result;
            if (bool.TryParse(s, out flag))
            {
                result = flag;
            }
            else
            {
                result = defaultVal;
            }
            return result;
        }

        public static bool? ToBoolean(string s)
        {
            bool value;
            bool? result;
            if (bool.TryParse(s, out value))
            {
                result = new bool?(value);
            }
            else
            {
                result = null;
            }
            return result;
        }

        public static byte ToByte(string s, byte defaultVal)
        {
            byte b;
            byte result;
            if (byte.TryParse(s, out b))
            {
                result = b;
            }
            else
            {
                result = defaultVal;
            }
            return result;
        }

        public static byte? ToByte(string s)
        {
            byte value;
            byte? result;
            if (byte.TryParse(s, out value))
            {
                result = new byte?(value);
            }
            else
            {
                result = null;
            }
            return result;
        }

        public static sbyte ToSByte(string s, sbyte defaultVal)
        {
            sbyte b;
            sbyte result;
            if (sbyte.TryParse(s, out b))
            {
                result = b;
            }
            else
            {
                result = defaultVal;
            }
            return result;
        }

        public static sbyte? ToSByte(string s)
        {
            sbyte value;
            sbyte? result;
            if (sbyte.TryParse(s, out value))
            {
                result = new sbyte?(value);
            }
            else
            {
                result = null;
            }
            return result;
        }

        public static char ToChar(string s, char defaultVal)
        {
            char c;
            char result;
            if (char.TryParse(s, out c))
            {
                result = c;
            }
            else
            {
                result = defaultVal;
            }
            return result;
        }

        public static char? ToChar(string s)
        {
            char value;
            char? result;
            if (char.TryParse(s, out value))
            {
                result = new char?(value);
            }
            else
            {
                result = null;
            }
            return result;
        }

        public static short ToShort(string s, short defaultVal)
        {
            short num;
            short result;
            if (short.TryParse(s, out num))
            {
                result = num;
            }
            else
            {
                result = defaultVal;
            }
            return result;
        }

        public static short? ToShort(string s)
        {
            short value;
            short? result;
            if (short.TryParse(s, out value))
            {
                result = new short?(value);
            }
            else
            {
                result = null;
            }
            return result;
        }

        public static ushort ToUShort(string s, ushort defaultVal)
        {
            ushort num;
            ushort result;
            if (ushort.TryParse(s, out num))
            {
                result = num;
            }
            else
            {
                result = defaultVal;
            }
            return result;
        }

        public static ushort? ToUShort(string s)
        {
            ushort value;
            ushort? result;
            if (ushort.TryParse(s, out value))
            {
                result = new ushort?(value);
            }
            else
            {
                result = null;
            }
            return result;
        }

        public static int ToInt(string s, int defaultVal)
        {
            int num;
            int result;
            if (int.TryParse(s, out num))
            {
                result = num;
            }
            else
            {
                result = defaultVal;
            }
            return result;
        }

        public static int? ToInt(string s)
        {
            int value;
            int? result;
            if (int.TryParse(s, out value))
            {
                result = new int?(value);
            }
            else
            {
                result = null;
            }
            return result;
        }

        public static uint ToUInt(string s, uint defaultVal)
        {
            uint num;
            uint result;
            if (uint.TryParse(s, out num))
            {
                result = num;
            }
            else
            {
                result = defaultVal;
            }
            return result;
        }

        public static uint? ToUInt(string s)
        {
            uint value;
            uint? result;
            if (uint.TryParse(s, out value))
            {
                result = new uint?(value);
            }
            else
            {
                result = null;
            }
            return result;
        }

        public static long ToLong(string s, long defaultVal)
        {
            long num;
            long result;
            if (long.TryParse(s, out num))
            {
                result = num;
            }
            else
            {
                result = defaultVal;
            }
            return result;
        }

        public static long? ToLong(string s)
        {
            long value;
            long? result;
            if (long.TryParse(s, out value))
            {
                result = new long?(value);
            }
            else
            {
                result = null;
            }
            return result;
        }

        public static ulong ToULong(string s, ulong defaultVal)
        {
            ulong num;
            ulong result;
            if (ulong.TryParse(s, out num))
            {
                result = num;
            }
            else
            {
                result = defaultVal;
            }
            return result;
        }

        public static ulong? ToULong(string s)
        {
            ulong value;
            ulong? result;
            if (ulong.TryParse(s, out value))
            {
                result = new ulong?(value);
            }
            else
            {
                result = null;
            }
            return result;
        }

        public static float ToFloat(string s, float defaultVal)
        {
            float num;
            float result;
            if (float.TryParse(s, out num))
            {
                result = num;
            }
            else
            {
                result = defaultVal;
            }
            return result;
        }

        public static float? ToFloat(string s)
        {
            float value;
            float? result;
            if (float.TryParse(s, out value))
            {
                result = new float?(value);
            }
            else
            {
                result = null;
            }
            return result;
        }

        public static double ToDouble(string s, double defaultVal)
        {
            double num;
            double result;
            if (double.TryParse(s, out num))
            {
                result = num;
            }
            else
            {
                result = defaultVal;
            }
            return result;
        }

        public static double? ToDouble(string s)
        {
            double value;
            double? result;
            if (double.TryParse(s, out value))
            {
                result = new double?(value);
            }
            else
            {
                result = null;
            }
            return result;
        }

        public static decimal ToDecimal(string s, decimal defaultVal)
        {
            decimal num;
            decimal result;
            if (decimal.TryParse(s, out num))
            {
                result = num;
            }
            else
            {
                result = defaultVal;
            }
            return result;
        }

        public static decimal? ToDecimal(string s)
        {
            decimal value;
            decimal? result;
            if (decimal.TryParse(s, out value))
            {
                result = new decimal?(value);
            }
            else
            {
                result = null;
            }
            return result;
        }

        public static DateTime ToDateTime(string s, DateTime defaultVal)
        {
            DateTime dateTime;
            DateTime result;
            if (DateTime.TryParse(s, out dateTime))
            {
                result = dateTime;
            }
            else
            {
                result = defaultVal;
            }
            return result;
        }

        public static DateTime? ToDateTime(string s)
        {
            DateTime value;
            DateTime? result;
            if (DateTime.TryParse(s, out value))
            {
                result = new DateTime?(value);
            }
            else
            {
                result = null;
            }
            return result;
        }

        public static T ToEnum<T>(string s, T defaultVal) where T : struct
        {
            Type typeFromHandle = typeof(T);
            if (!typeFromHandle.IsEnum)
            {
                throw new NotSupportedException(string.Format("类型{0}不是枚举", typeFromHandle));
            }
            T result;
            try
            {
                result = (T)Enum.Parse(typeFromHandle, s);
            }
            catch
            {
                result = defaultVal;
            }
            return result;
        }
    }
}